package com.demo.controller;

import com.demo.HelloApplication;
import com.demo.util.ControllerManager;
import com.demo.util.EmailSender;
import com.demo.util.Tabs;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;

import javax.mail.*;
import javax.mail.internet.MimeBodyPart;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ResourceBundle;

import static com.demo.util.ControllerManager.leftController;

public class Step2Controller implements Initializable {
    private ArrayList<Message> messageList;
    private int index;
    @FXML
    private TextField subject;
    @FXML
    private AnchorPane ap;
    @FXML
    private TextField to;
    @FXML
    private TextField cc;
    @FXML
    private Label attachmentPath;
    @FXML
    private HTMLEditor content;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        // 邮件预览
        System.out.println("Step2Controller.Init");
        messageList = EmailSender.getMessageList();
        ControllerManager.step2Controller = this;
        index = 0;
        showMail(index);
    }

    private void showMail(int index) {
        try {
            // 邮件标题
            String subject = messageList.get(index).getSubject();
            this.subject.setText(subject);
            // 收件人
            Address[] address1 = messageList.get(index).getRecipients(Message.RecipientType.TO);
            this.to.setText(Arrays.toString(address1));
            // 抄送
            Address[] address2 = messageList.get(index).getRecipients(Message.RecipientType.CC);
            this.cc.setText(Arrays.toString(address2));
            // 读取附件、正文
            Object content = messageList.get(index).getContent();
            String plainContent = "";
            String filePath = "";
            if (content instanceof Multipart) {
                Multipart multipart = (Multipart) content;
                    for (int i = 0; i < multipart.getCount(); i++) {
                        MimeBodyPart part = (MimeBodyPart) multipart.getBodyPart(i);
                        //System.out.println(part.getContentType());
                        // !这里get有bug，始终是text/plain

                        if (part.getFileName() != null) {
                            filePath=part.getFileName();
                        }
                        if (part.getContent() instanceof String) {
                            plainContent = (String) part.getContent();
                        }
                }
            } else {
                plainContent = (String) content;
            }

            this.attachmentPath.setText(filePath);

            // 正文
            this.content.setHtmlText(plainContent);
        } catch (MessagingException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    @FXML
    protected void onNextMailClick() {
        if (index + 1 < messageList.size()) {
            index++;
            showMail(index);
        }
    }

    @FXML
    protected void onPrveMailClick() {
        if (index - 1 >= 0) {
            index--;
            showMail(index);
        }
    }

    @FXML
    protected void onBackStepClick() { //返回
        Stage stage = (Stage) ap.getScene().getWindow();
        stage.setTitle("Step1!");
        Scene scene1 = Tabs.getScene(Tabs.STEP1_KEY);
        stage.setScene(scene1);
        stage.show();
        leftController.setCurrentStep(1);
    }

    @FXML
    protected void onNextStepClick() throws MessagingException, IOException { //发送
        leftController.setCurrentStep(3);
        EmailSender.sendEmail();

        // 创建step3
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("step3-view.fxml"));
        Scene newScene = new Scene(fxmlLoader.load());
        leftController.setCurrentStep(3);

        Stage stage = (Stage) ap.getScene().getWindow();
        stage.setTitle("Step3!");
        stage.setScene(newScene);
        stage.show();
    }

}
